package com.xbfinal.ggkt.vod.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbfinal.ggkt.exception.GgktException;
import com.xbfinal.ggkt.model.vod.Teacher;
import com.xbfinal.ggkt.result.Result;
import com.xbfinal.ggkt.vo.vod.TeacherQueryVo;
import com.xbfinal.ggkt.vod.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author xbfinal
 * @since 2022-11-21
 */
@Api(tags = "讲师管理")
@RestController
@CrossOrigin
@RequestMapping("/admin/vod/teacher")
public class TeacherController {

    @Resource
    private TeacherService teacherService;


    /**
     * 查询所有讲师
     */
    @ApiOperation("查询所有讲师")
    @GetMapping("findAll")
    //    public List<Teacher> findAllTeacher(){
    //        //调用service方法
    //        return teacherService.list();
    //    }
    public Result<List<Teacher>> findAllTeacher(){

        try{
            int i=1/0;
        }catch (Exception e){
            //手动抛出异常
            throw new GgktException(201,"执行了自定义异常处理");
        }
        List<Teacher> teacherList = teacherService.list();
        return Result.ok(teacherList);
    }

    /**
     * 逻辑删除
     */
    @ApiOperation("逻辑删除讲师")
    @DeleteMapping("remove/{id}")
    //    public boolean reMoveTeacher(@PathVariable("id") Long id){
    //        return teacherService.removeById(id);
    //    }
    public Result reMoveTeacher(@PathVariable("id") Long id){
        boolean removeById = teacherService.removeById(id);
        if (removeById){
            return  Result.ok(null);
        }
        return  Result.fail(null);
    }
    /**
     * 条件查询分页
     * current当前页
     * limit每页的条数
     */
    @ApiOperation("条件查询分页")
    @GetMapping("findQueryPage/{current}/{limit}")
    public Result findQueryPage(
            @PathVariable("current") long current,
            @PathVariable("limit") long limit,
            TeacherQueryVo teacherQueryVo){
        //逻辑写在service
        Page<Teacher>page=teacherService.findQueryPage(current,limit,teacherQueryVo);
        return Result.ok(page);
    }

    /**
     * 添加讲师
     */
    @ApiOperation("添加讲师")
    @PostMapping("saveTeacher")
    public Result saveTeacher(@RequestBody Teacher teacher){

        boolean save = teacherService.save(teacher);
        if(save){
            return  Result.ok(null);
        }
        return Result.fail("添加失败");
    }
    /**
     * 上传讲师图片
     *
     */



    /**
     * 根据id查询讲师
     */
    @ApiOperation("根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public Result findById(@PathVariable Long id){
        Teacher teacher = teacherService.getById(id);
        return  Result.ok(teacher);
    }
    /**
     * 修改-根据id修改
     */
    @ApiOperation("修改讲师-根据id修改")
    @PostMapping("updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.updateById(teacher);
        if(isSuccess){
            return  Result.ok(null);
        }
        return Result.fail("修改失败");
    }
    /**
     * 批量删除讲师
     */
    @ApiOperation("批量删除讲师")
    @DeleteMapping("removeBatch")
    public Result removeBatch(@RequestBody List<Long> id){
        boolean isSuccess = teacherService.removeByIds(id);
        if(isSuccess){
            return  Result.ok(null);
        }
        return Result.fail("删除失败");
    }

}

